//
//  HTCalloutView.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/17.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTCalloutView.h"
#import "UIImageView+WebCache.h"
//210*188
@interface HTCalloutView ()
@property (weak, nonatomic) IBOutlet UIButton *callButton;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *licenceLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *placeLabel;

@end
@implementation HTCalloutView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    self = [[[NSBundle mainBundle] loadNibNamed:@"HTCalloutView" owner:self options:nil] lastObject];
    if (self) {
        self.frame = frame;
        self.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.3];
        self.headImageView.layer.masksToBounds = YES;
        self.headImageView.layer.cornerRadius = 21.5;
    }
    return self;
}

- (void)setDriverMainModel:(HTDriverMainModel *)driverMainModel{
    _driverMainModel = driverMainModel;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:driverMainModel.pic] placeholderImage:[UIImage imageNamed:@"bg_headsculpture"]options:SDWebImageRefreshCached];
    self.nameLabel.text = driverMainModel.name;
    [self.nameLabel setValue:driverMainModel.name forKey:@"text"];
    self.licenceLabel.text = driverMainModel.carnum;
    self.typeLabel.text = [NSString stringWithFormat:@"%@米/%@",driverMainModel.length,driverMainModel.types];
    if ([driverMainModel.isoffer isEqualToString: [NSString stringWithFormat:@"%d",YES]]) {
        //是熟车
        [self.addButton setTitle:@"取消熟车" forState:UIControlStateNormal];
    }else{
        //不是熟车
        [self.addButton setTitle:@"添加熟车" forState:UIControlStateNormal];
    }
    self.placeLabel.text = driverMainModel.reuslt;
    self.timeLabel.text = [HTUtils timeIntervalExchange:driverMainModel.createtime];
}

- (IBAction)didClickCallButton:(UIButton *)sender {
    
    if (self.driverMainModel == nil || self.driverMainModel.phone == nil || self.driverMainModel.phone.length <= 0) {
        [HTShowUtil showMessage:@"司机信息请求失败"];
        return;
    }
    NSString *phone = self.driverMainModel.phone;
    NSString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    
}
- (IBAction)didClickAddButton:(UIButton *)sender {
    
    if (self.driverMainModel == nil || self.driverMainModel.carid == nil || self.driverMainModel.carid.length <= 0) {
        [HTShowUtil showMessage:@"司机信息请求失败"];
        return;
    }
    if ([sender.titleLabel.text isEqualToString:@"取消熟车"]) {
        [self netLoadDataDeleteTruck];
    }else{
        [self netLoadDataAddTruck];
    }
    
}
- (void)netLoadDataAddTruck{
    [HTHttpRequestManager requestTruckAddWithCarid:self.driverMainModel.carid success:^(id  _Nullable responseObject) {
        DBLog(@"添加熟车：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"添加熟车失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        [self.addButton setTitle:@"取消熟车" forState:UIControlStateNormal];
    } failure:^(NSError * _Nonnull error) {
        [HTShowUtil showMessage:@"添加熟车失败"];
    }];
}
- (void)netLoadDataDeleteTruck{
    [HTHttpRequestManager requestTruckDeleteWithCarid:self.driverMainModel.carid success:^(id  _Nullable responseObject) {
        DBLog(@"删除熟车：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"删除熟车失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        [self.addButton setTitle:@"添加熟车" forState:UIControlStateNormal];
    } failure:^(NSError * _Nonnull error) {
        [HTShowUtil showMessage:@"删除熟车失败"];
    }];
}
@end
